package com.liu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liu.dao.DepartmentDao;
import com.liu.dto.DepartmentCountDto;
import com.liu.entity.Department;
import com.liu.service.intf.DepartmentService;
import com.liu.vo.PageBeanJson;
import com.liu.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao dao;
    @Override
    public R save(Department department) {
        //执行新增操作

        if(dao.add(department)>0){
            //成功
            return R.ok();
        }else{
            //失败
            return R.fail();
        }
    }

    @Override
    public PageBeanJson queryPage(int page, int limit) {
        //1.分页
        PageHelper.startPage(page, limit);
        //2.获取分页结果

        PageInfo<Department> pageInfo=new PageInfo<>(dao.selectAll());
        //3.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R del(int id) {
        //1.验证参数
        if(id>0){
            //2.操作数据库 验证是否成功
            if(dao.del(id)>0){
                //删除成功
                return R.ok();
            }else{
                return R.fail();
            }
        }else{
            return R.fail();
        }
    }

    @Override
    public R update(Department department) {
        if(dao.update(department)>0){
            return R.ok();
        }else{
            return R.fail();
        }
    }

    @Override
    public PageBeanJson queryPageWhere(String depname, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(depname)){
            depname=null;
        }else{
            depname="%"+depname+"%";//包含  模糊查询
        }
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果

        PageInfo<Department> pageInfo=new PageInfo<>(dao.selectWhere(depname));
        //4.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public R delBatch(int[] ids) {
        //1.验证参数
        if(ids!=null && ids.length>0){
            //2.执行
            if(dao.delBatch(ids)>0){
                return R.ok();
            }
        }
        return R.fail();
    }

    @Override
    public PageBeanJson selectCount(int page, int limit) {
        //2.分页
        PageHelper.startPage(page, limit);
        //3.获取分页结果


        PageInfo<DepartmentCountDto> pageInfo=new PageInfo<>(dao.selectCount());
        //4.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }

    @Override
    public PageBeanJson selectCountbyName(String dptname, int page, int limit) {
        //1.验证 查询条件 如果为 "" 那么赋值为null
        if(!StringUtils.hasLength(dptname)){
            dptname=null;
        }else{
            dptname="%"+dptname+"%";//包含  模糊查询
        }

        PageInfo<DepartmentCountDto> pageInfo=new PageInfo<>(dao.selectCountByName(dptname));
        //4.返回分页的数据
        return PageBeanJson.bean(pageInfo.getTotal(), pageInfo.getList());
    }
}

